package com.ihelp.rest.mis.platform.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ihelp.rest.mis.platform.service.SiteService;
import com.ihelp.rest.mis.pojo.Site;
import com.ihelp.rest.mis.pojo.SiteType;
import com.ihelp.rest.utils.HtmlUtil;



@Controller
@RequestMapping("/system/site")
public class SiteController {
	
	@Autowired
	private SiteService siteservice;
	
	//网址库分类管理页面
	@RequestMapping("/sitetype/sitetypemanage.do")
	public String loadSiteTypeManage(HttpServletRequest request){
		return "system/site/sitetype/sitetypemanage";
	}
	
	//网址库管理页面
	@RequestMapping("/site/sitemanage.do")
	public String loadSiteManage(HttpServletRequest request){
		return "system/site/site/sitemanage";
	}
	
	
	/**
	 * 跳转网站分类添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/sitetype/sitetypeadd.do")
	public String loadSiteTypeAdd(HttpServletRequest request, String id,String pid){
		Map<String, String> ref = new HashMap<String, String>();
		if(null==pid&&id!=null){//修改
			request.setAttribute("SiteType", this.siteservice.getSiteTypeById(id));
			Map<String, String> m = new HashMap<String, String>();
			SiteType p_st = this.siteservice.getSiteTypeByCode(this.siteservice.getSiteTypeById(id).getParentcode());
			if(null==p_st){
				m.put("name", "无上级");
			}else{
				m.put("name", p_st.getTypename());
			}
			ref.put("saveType", "modify");
			request.setAttribute("Name", m );
			
		}
		if(null==id&&pid!=null){//新增
			SiteType new_st = this.siteservice.getSiteTypeByCode(pid);
			Map<String, String> m = new HashMap<String, String>();
			Map<String, String> name = new HashMap<String, String>();
			m.put("code", pid);
			if(null==new_st){
				name.put("name", "无上级");
				m.put("parentcode", "0");
			}else{
				name.put("name", new_st.getTypename());
				m.put("parentcode", pid);
			}
			String next_code = this.siteservice.getNextCode(pid);
			if(StringUtils.isBlank(next_code)){
				next_code = pid+"01";
			}
			m.put("typecode", next_code);
			request.setAttribute("SiteType", m );
			request.setAttribute("Name",name );
		}
		request.setAttribute("ref", ref);
		return "system/site/sitetype/sitetypeadd";
	}
	
	/**
	 * 网站分类列表
	 * @param request
	 */
	@RequestMapping("/sitetype/jsonSitetypePageList.do")
	public void loadSiteTypeList(HttpServletResponse response, SiteType queryCondition,int page,int rows,String sort, String order){
		String json = this.siteservice.findSiteTypePage(queryCondition, page, rows, sort, order);
		HtmlUtil.writerJson(response, json);
	}
	
	
	/**
	 * 删除网站分类
	 * @param response
	 * @param module
	 */
	@RequestMapping("/sitetype/delSiteType.do")
	public void delMSiteType(HttpServletResponse response, String id){
		String result = this.siteservice.delSiteType(id);
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	
	/**
	 * 获取网站分类Tree  
	 * @param response
	 */
	@RequestMapping("/sitetype/findSiteTypeTree.do")
	public void findSiteTypeTree(HttpServletResponse response,String parentcode){
		String json = this.siteservice.findSiteTypeTree(parentcode);
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 保存网站分类   
	 * @param response
	 */
	@RequestMapping("/sitetype/saveSiteType.do")
	public void addSiteType(Model model,HttpServletResponse response, HttpSession session, SiteType sitetype){
		String result = new String(); 
		result= this.siteservice.addSiteType(sitetype); 
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	/**
	 * 网站列表
	 * 
	 * */
	@RequestMapping("/site/findSitePage.do")
	public void findModulePage(HttpServletResponse response, int page, int rows,String typecode){
		String json = this.siteservice.findPageAllSites(page, rows,typecode);
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 新增网站记录
	 * */
	@RequestMapping("/site/siteadd.do")
	public String loadSite(HttpServletRequest request,String id){
		if(StringUtils.isNotBlank(id)){
			request.setAttribute("Site", this.siteservice.getSiteById(id));
		}
		return "system/site/site/siteadd";
	}
	
	/**
	 * 保存网站分类   
	 * @param response
	 */
	@RequestMapping("/site/saveSite.do")
	public void saveSite(Model model,HttpServletResponse response, HttpSession session, Site site){
		String result = new String(); 
		result= this.siteservice.saveSite(site); 
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	
	@RequestMapping("/site/delSite.do")
	public void delSite(HttpServletResponse response, String id){
		String result = this.siteservice.delSite(id);
		HtmlUtil.sendDirectToClient(response,result);
	}
	
}
